//
//  TixianJiluViewController.swift
//  联付商圈
//
//  Created by zhangqi on 2017/8/18.
//  Copyright © 2017年 lianfu. All rights reserved.
//

import UIKit
import Alamofire

class TixianJiluViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    
    var show = 1
    var listArray = ["全部","近一个月","近三个月","近六个月"]
    var dic = [[String:Any]]()
    
    
    
    
    
    
    
   
    
    
    @IBOutlet weak var choutiTableView: UITableView!
    
    
    @IBOutlet weak var totalTableView: UITableView!
    
    @IBOutlet weak var backgroundImage: UIImageView!
    
    
    
    
    
    
    
    
    
    @IBOutlet weak var tixianmingxiBtn: UIButton!
    
    @IBAction func tixianmingxiBtn(_ sender: UIButton) {
        if show == 2 {
            hid()
            tixianmingxiBtn.setImage(UIImage(named:"提下"), for:.normal)
        } else if show == 1 {
            UITableView.animate(withDuration: 0.2, animations: {
                self.choutiTableView.frame = CGRect(x: 0, y: 64, width: mainScreenW, height:55*4)
                self.backgroundImage.alpha = 0.6
            })
            tixianmingxiBtn.setImage(UIImage(named:"提上"), for:.normal)
            show = 2
        }

    }
    
    func hid() {
        UITableView.animate(withDuration: 0.2, animations: {
            self.backgroundImage.alpha = 0
            self.choutiTableView.frame = CGRect(x: 0, y: -55*7+64, width: mainScreenW, height:55*4)
        })
        show = 1
    }
    func touch() {
        let tap = UITapGestureRecognizer(target : self,action : #selector(hid))
        tap.numberOfTapsRequired = 1
        backgroundImage.isUserInteractionEnabled = true
        backgroundImage.addGestureRecognizer(tap)
        
    }
    
    
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        choutiTableView.tag = 101
        totalTableView.tag = 102
        //choutiTableView.
        choutiTableView.showsVerticalScrollIndicator = false
        totalTableView.tableFooterView = UIView(frame:CGRect.zero)
        totalTableView.showsVerticalScrollIndicator = false
        choutiTableView.frame = CGRect(x: 0, y: -55*7+64, width: mainScreenW, height:55*4)
        //touch()
        tixianjilu()
    }

    override func viewWillAppear(_ animated: Bool) {
        //self.navigationController?.navigationBar.isHidden = true
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    func tixianjilu(){
        let x = UserDefaults.standard.object(forKey: "userInfo") as! NSDictionary
        let y = x["name"]!
        let yhid = ["name":"\(y)"]
        let urlString = "https://www.beidikeji.com/life/public/jiekou/showtixian?"
        let manager = Alamofire.SessionManager.default
        manager.session.configuration.timeoutIntervalForRequest = 45
        manager.request(urlString,method:.post,parameters: yhid).responseJSON {Request in
            switch Request.result {
            case .success:
                let json = JSON(data: Request.data!).arrayObject
                self.dic = json as! [[String : Any]]
                self.totalTableView.reloadData()
            //self.noticeInfo("编辑成功", autoClear: true, autoClearTime: 1)
            case .failure:
                self.noticeInfo("未知错误", autoClear: true, autoClearTime: 1)
            }
        }
    }

    

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView.tag == 101 {
            return listArray.count
        } else if tableView.tag == 102 {
            return dic.count
        } else {
            return 0
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if tableView.tag == 101 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "TixianChoutiTableViewCell", for: indexPath) as! TixianChoutiTableViewCell
            cell.titleLab.text = listArray[indexPath.row]
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "TixianTableViewCell", for: indexPath) as! TixianTableViewCell
            cell.oneLab.text = "提现时间:" + "\(dic[indexPath.row]["created_time"]!)"
            cell.twoLab.text = "¥" + "\(dic[indexPath.row]["tixianmoney"]!)"
            let y = "\(dic[indexPath.row]["BankCardNumber"]!)"
            let startIndex = y.index(y.startIndex, offsetBy: y.lengthOfBytes(using: .utf8) - 4)
            let endIndex = y.index(startIndex, offsetBy:4)
            let result = y.substring(with: startIndex..<endIndex)
            //cell.label.text = x + result

            cell.threeLab.text = "\(dic[indexPath.row]["bank"]!)" + result
            return cell
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//        if tableView.tag == 101 {
//            switch indexPath.row {
//            case 0:
//                hid()
//            case 1:
//                hid()
//            case 2:
//                hid()
//            case 3:
//                hid()
//            default:
//                break
//            }
//        }
        
    }


}
